package com.mcxx.convert;

import com.alibaba.fastjson.JSON;
import com.mcxx.constant.DictConstant;
import com.mcxx.formulcount.param.FormulCountParam;
import com.mcxx.modules.base.service.read.DictionaryReadService;
import com.mcxx.modules.miniSalvation.order.dto.ExOrderSalaryDTO;
import com.mcxx.modules.miniSalvation.salary.dto.FamilyMemberSalaryDTO;
import com.mcxx.modules.miniSalvation.order.dto.OrderSalaryDTO;
import com.mcxx.modules.miniSalvation.salary.entity.FamilySalaryEntity;
import com.mcxx.modules.miniSalvation.salary.entity.MemberSalaryEntity;
import com.mcxx.modules.temporary.family.dto.TmpFamilyMemberDTO;
import com.mcxx.modules.temporary.order.dto.TmpAuditDTO;
import com.mcxx.modules.temporary.salary.entity.TmpFamilySalaryEntity;
import com.mcxx.modules.temporary.salary.entity.TmpMemberSalaryEntity;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.utils.ServiceLocator;
import com.yinhai.ta404.module.dict.entity.TaDictPo;
import java.util.Date;
import java.util.Map;


public class FormulCountParamConvert {




    public static FormulCountParam convertOrderSalaryDTO(OrderSalaryDTO orderSalaryDTO, String countryOrgId){
        FormulCountParam formulCountParam = new FormulCountParam();

        formulCountParam.setSaveMemberNumber(orderSalaryDTO.getSaveMemberNumber());

        formulCountParam.setDisabilityNumber(orderSalaryDTO.getFamilyDisabilityCnt());

        formulCountParam.setAreaCode(orderSalaryDTO.getAreaCode().substring(0,6));

        formulCountParam.setBusinessType(orderSalaryDTO.getBusinessType());

        formulCountParam.setFormulaValue(orderSalaryDTO.getCountType());

        formulCountParam.setType(orderSalaryDTO.getStandType());


        DictionaryReadService dictionaryReadService = ServiceLocator.getAppContext().getBean(DictionaryReadService.class);
        TaDictPo dictPo = dictionaryReadService.getCodeCache(DictConstant.ASSISTANT_STANDARD_CITY_OR_COUNTY, orderSalaryDTO.getStandType());

        formulCountParam.setTypeName(dictPo.getLabel());

        formulCountParam.setTaOrgId(countryOrgId);

        return formulCountParam;
    }


    public static void convertMemberSalaryDTO(FamilyMemberSalaryDTO familyMemberSalary, FormulCountParam formulCountParam){

        DictionaryReadService dictionaryReadService = ServiceLocator.getAppContext().getBean(DictionaryReadService.class);
        TaDictPo dictPo;

        if(!CommonUtil.invalidArgs(familyMemberSalary.getDisabilityType())){
            dictPo = dictionaryReadService.getCodeCache(DictConstant.DISABLE_TYPE, familyMemberSalary.getDisabilityType());
            formulCountParam.setDisabilityType(dictPo.getLabel());
        }else{
            formulCountParam.setDisabilityType(null);
        }

        if(!CommonUtil.invalidArgs(familyMemberSalary.getDisabilityLevel())){

            dictPo = dictionaryReadService.getCodeCache(DictConstant.DISABLED_LEVEL, familyMemberSalary.getDisabilityLevel());
            formulCountParam.setDisabilityLevel(dictPo.getLabel());
        }else{
            formulCountParam.setDisabilityLevel(null);
        }

        if(!CommonUtil.invalidArgs(familyMemberSalary.getHealthState())){

            dictPo = dictionaryReadService.getCodeCache(DictConstant.HEALTH, familyMemberSalary.getHealthState());
            formulCountParam.setHealth(dictPo.getLabel());
        }else{
            formulCountParam.setHealth(null);
        }

        if(!CommonUtil.invalidArgs(familyMemberSalary.getSeriousDisease())){

            dictPo = dictionaryReadService.getCodeCache(DictConstant.SERIOUS_DISEASE, familyMemberSalary.getSeriousDisease());
            formulCountParam.setSeverediseases(dictPo.getLabel());
        }else{
            formulCountParam.setSeverediseases(null);
        }


        formulCountParam.setOneself(familyMemberSalary.getSelfcareLevel());

        if(!CommonUtil.invalidArgs(familyMemberSalary.getEducation())){

            dictPo = dictionaryReadService.getCodeCache(DictConstant.EDUCATION_LEVEL, familyMemberSalary.getEducation());
            formulCountParam.setEducation(dictPo.getLabel());
        }else{
            formulCountParam.setEducation(null);
        }

        if(!CommonUtil.invalidArgs(familyMemberSalary.getIsAtSchool())){

            dictPo = dictionaryReadService.getCodeCache(DictConstant.IS_AT_SCHOOL, familyMemberSalary.getIsAtSchool());
            formulCountParam.setIsAtSchool(dictPo.getLabel());
        }else{
            formulCountParam.setIsAtSchool(null);
        }


        if(!CommonUtil.invalidArgs(familyMemberSalary.getAge())) {
            formulCountParam.setAge(familyMemberSalary.getAge() + "");
        }
        else{
            formulCountParam.setAge(null);
        }
    }


    public static MemberSalaryEntity convertMemberSalaryEntity(OrderSalaryDTO orderSalaryDTO, FamilyMemberSalaryDTO familyMemberSalary,
                                                               double memberSalaryNumber, String itemType){
        MemberSalaryEntity entity = new MemberSalaryEntity();
        entity.setId(Constant.getUUID());
        entity.setBusinessType(orderSalaryDTO.getBusinessType());
        entity.setAreaCode(orderSalaryDTO.getAreaCode());
        entity.setCountyCode(orderSalaryDTO.getAreaCode().substring(0,6));
        entity.setSalaryType(Constant.SalaryType.ZHENGCHANG);
        entity.setMemberId(familyMemberSalary.getMemberId());
        entity.setFamilyId(orderSalaryDTO.getFamilyId());
        entity.setOrderId(orderSalaryDTO.getOrderId());
        entity.setSalaryNumber(memberSalaryNumber);

        entity.setEndDate(Constant.MAX_YEAR_DATE);

        entity.setItemType(itemType);

        entity.setSaveReason(orderSalaryDTO.getReason());
        return entity;
    }

    public static FamilySalaryEntity convertFamilySalaryEntity(OrderSalaryDTO orderSalaryDTO, double memberSalaryTotal ,double sortSalaryTotal,String masterSave,
                                                               double standardMoney){
        FamilySalaryEntity entity=new FamilySalaryEntity();
        entity.setId(Constant.getUUID());
        entity.setBusinessType(orderSalaryDTO.getBusinessType());


        if(orderSalaryDTO.getAreaCode() != null && !"".equals(orderSalaryDTO.getAreaCode())){
            entity.setAreaCode(orderSalaryDTO.getAreaCode());
            entity.setCountyCode(orderSalaryDTO.getAreaCode().substring(0,6));
        }
        entity.setSalaryType(Constant.SalaryType.ZHENGCHANG);
        entity.setEndDate(Constant.MAX_YEAR_DATE);
        entity.setInputDate(DateUtil.getMonthString());

        entity.setSalaryNumber(memberSalaryTotal);
        entity.setSortSalary(sortSalaryTotal);

        entity.setFamilyId(orderSalaryDTO.getFamilyId());
        entity.setOrderId(orderSalaryDTO.getOrderId());

        entity.setSaveReason(orderSalaryDTO.getReason());

        entity.setIdcard(orderSalaryDTO.getIdcard());

        entity.setCalcType(orderSalaryDTO.getCountType());

        entity.setMasterInSave(masterSave);

        entity.setChangeTime(new Date());

        entity.setChangeType(Constant.FamilySalaryChangeType.CHANGE);
        entity.setStandNumber(standardMoney);
        return entity;
    }






    public static FormulCountParam convertOrderSalaryDTO(ExOrderSalaryDTO orderSalaryDTO, String parentOrgId){
        FormulCountParam formulCountParam = new FormulCountParam();

        formulCountParam.setSaveMemberNumber(orderSalaryDTO.getSaveMemberNumber());

        formulCountParam.setDisabilityNumber(orderSalaryDTO.getFamilyDisabilityCnt());

        formulCountParam.setAreaCode(orderSalaryDTO.getAreaCode().substring(0,6));

        formulCountParam.setBusinessType(orderSalaryDTO.getBusinessType());

        formulCountParam.setType(orderSalaryDTO.getStandType());

        formulCountParam.setProvide(orderSalaryDTO.getSupplyType());


        DictionaryReadService dictionaryReadService = ServiceLocator.getAppContext().getBean(DictionaryReadService.class);
        TaDictPo dictPo = dictionaryReadService.getCodeCache(DictConstant.ASSISTANT_STANDARD_CITY_OR_COUNTY, orderSalaryDTO.getStandType());

        formulCountParam.setTypeName(dictPo.getLabel());

        formulCountParam.setTaOrgId(parentOrgId);
        return formulCountParam;
    }



    public static MemberSalaryEntity convertMemberSalaryEntity(ExOrderSalaryDTO orderSalaryDTO, FamilyMemberSalaryDTO familyMemberSalary,
                                                               double memberSalaryNumber, String itemType){
        MemberSalaryEntity entity=new MemberSalaryEntity();
        entity.setId(Constant.getUUID());
        entity.setBusinessType(orderSalaryDTO.getBusinessType());
        entity.setAreaCode(orderSalaryDTO.getAreaCode());
        entity.setCountyCode(orderSalaryDTO.getAreaCode().substring(0,6));

        entity.setSalaryType(Constant.SalaryType.ZHENGCHANG);
        entity.setMemberId(familyMemberSalary.getMemberId());
        entity.setFamilyId(orderSalaryDTO.getFamilyId());
        entity.setOrderId(orderSalaryDTO.getOrderId());
        entity.setSalaryNumber(memberSalaryNumber);

        entity.setEndDate(Constant.MAX_YEAR_DATE);

        entity.setItemType(itemType);

        entity.setSupplyOrgId(orderSalaryDTO.getSupplyOrgId());

        entity.setSupplyType(orderSalaryDTO.getSupplyType());

        entity.setSaveReason(orderSalaryDTO.getReason());
        return entity;
    }


    public static FamilySalaryEntity convertFamilySalaryEntity(ExOrderSalaryDTO orderSalaryDTO, double memberSalaryTotal, String masterSave,
                                                               double baseSalvNumber, double nursingNumber, double standardMoney){
        FamilySalaryEntity entity = new FamilySalaryEntity();
        entity.setId(Constant.getUUID());
        entity.setBusinessType(orderSalaryDTO.getBusinessType());


        if(orderSalaryDTO.getAreaCode() != null && !"".equals(orderSalaryDTO.getAreaCode())){
            entity.setAreaCode(orderSalaryDTO.getAreaCode());
            entity.setCountyCode(orderSalaryDTO.getAreaCode().substring(0,6));
        }
        entity.setSalaryType(Constant.SalaryType.ZHENGCHANG);
        entity.setEndDate(Constant.MAX_YEAR_DATE);
        entity.setInputDate(DateUtil.getMonthString());

        entity.setSalaryNumber(memberSalaryTotal);
        entity.setFamilyId(orderSalaryDTO.getFamilyId());
        entity.setOrderId(orderSalaryDTO.getOrderId());

        entity.setSupplyOrgId(orderSalaryDTO.getSupplyOrgId());

        entity.setSupplyType(orderSalaryDTO.getSupplyType());

        entity.setSaveReason(orderSalaryDTO.getReason());

        entity.setIdcard(orderSalaryDTO.getIdcard());

        entity.setCalcType(orderSalaryDTO.getSupplyType());

        entity.setMasterInSave(masterSave);

        entity.setBaseSalvNumber(baseSalvNumber);

        entity.setNursingNumber(nursingNumber);

        entity.setChangeTime(new Date());

        entity.setChangeType(Constant.FamilySalaryChangeType.CHANGE);

        entity.setStandNumber(standardMoney);
        return entity;
    }







    public static TmpFamilySalaryEntity convertFamilySalaryEntity(TmpAuditDTO tmpAuditDTO){
        TmpFamilySalaryEntity entity=new TmpFamilySalaryEntity();
        entity.setId(Constant.getUUID());

        entity.setFamilyId(tmpAuditDTO.getFamilyId());

        entity.setOrderId(tmpAuditDTO.getOrderId());

        entity.setBusinessType(tmpAuditDTO.getBusinessType());

        if(tmpAuditDTO.getAreaCode() != null && !"".equals(tmpAuditDTO.getAreaCode())){
            entity.setAreaCode(tmpAuditDTO.getAreaCode());
            entity.setCountyCode(tmpAuditDTO.getAreaCode().substring(0,6));
        }

        entity.setStartDate(tmpAuditDTO.getRescueyears());
        entity.setEndDate(tmpAuditDTO.getRescueyears());

        entity.setInputDate(DateUtil.getMonthString());

        entity.setSalaryNumber(tmpAuditDTO.getSalvMoney());

        entity.setGoodsType(tmpAuditDTO.getGoodsType());

        entity.setKinds(JSON.toJSONString(tmpAuditDTO.getGoodsList()));

        entity.setGovReferralMoney(tmpAuditDTO.getGovReferralMoney());

        entity.setGovUnionMoney(tmpAuditDTO.getGovUnionMoney());

        entity.setIdcard(tmpAuditDTO.getIdcard());
        return entity;
    }


    public static TmpMemberSalaryEntity convertMemberSalaryEntity(TmpAuditDTO tmpAuditDTO, TmpFamilyMemberDTO familyMember){
        TmpMemberSalaryEntity entity=new TmpMemberSalaryEntity();
        entity.setId(Constant.getUUID());

        entity.setFamilyId(tmpAuditDTO.getFamilyId());

        entity.setOrderId(tmpAuditDTO.getOrderId());

        entity.setMemberId(familyMember.getId());

        if(tmpAuditDTO.getAreaCode() != null && !"".equals(tmpAuditDTO.getAreaCode())){
            entity.setAreaCode(tmpAuditDTO.getAreaCode());
            entity.setCountyCode(tmpAuditDTO.getAreaCode().substring(0,6));
        }

        entity.setStartDate(tmpAuditDTO.getRescueyears());
        entity.setEndDate(tmpAuditDTO.getRescueyears());

        entity.setInputDate(DateUtil.getMonthString());

        entity.setItemType(Constant.CareFeeType.LINSHI_BASE_CODE);

        double yusu = tmpAuditDTO.getSalvMoney() % tmpAuditDTO.getSaveMemberNumber();
        double memberSalaryNumber = (tmpAuditDTO.getSalvMoney() - yusu) / tmpAuditDTO.getSaveMemberNumber();

        memberSalaryNumber += Constant.DomesticRelation.ONESELF.equals(familyMember.getFamilyRelation()) ? yusu : 0;
        entity.setSalaryNumber(memberSalaryNumber);

        return entity;
    }


    public static FormulCountParam convertOrderSalaryDTO(Map<String, Double> masterSocietyNumMap, UserAccountVo userAccountVo, TmpAuditDTO tmpAuditDTO){
        FormulCountParam formulCountParam = new FormulCountParam();

        formulCountParam.setCumulative(masterSocietyNumMap.get("societyNum") + "");

        formulCountParam.setAccruing(CommonUtil.invalidArgs(masterSocietyNumMap.get("societySalary")) ? "0" : masterSocietyNumMap.get("societySalary") + "");

        formulCountParam.setAreaCode(tmpAuditDTO.getAreaCode().substring(0,6));

        formulCountParam.setBusinessType(Constant.BusinessCode.LINSHI);

        formulCountParam.setTaOrgId(userAccountVo.getParentId());

        formulCountParam.setFormulaValue(tmpAuditDTO.getSalvType());

        DictionaryReadService dictionaryReadService = ServiceLocator.getAppContext().getBean(DictionaryReadService.class);
        TaDictPo dictPo = dictionaryReadService.getCodeCache(DictConstant.TempasstCountType, tmpAuditDTO.getSalvType());
        formulCountParam.setTypeName(dictPo.getLabel());

        formulCountParam.setKindValue(tmpAuditDTO.getGoodsType());

        formulCountParam.setKind(JSON.toJSONString(tmpAuditDTO.getGoodsList()));

        formulCountParam.setPovertyValue(tmpAuditDTO.getMainReason());

        return formulCountParam;
    }


}
